package com.zhucan.account.infrastructure.exception.advice;

import com.zhucan.account.application.query.dto.ResultDTO;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.persistence.EntityNotFoundException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: zhuCan
 * @date: 2020/8/3 22:30
 * @description:
 */
@RestControllerAdvice
public class EntityNotFoundExceptionAdvice {

  @ExceptionHandler(EntityNotFoundException.class)
  @ResponseStatus(HttpStatus.NOT_FOUND)
  public ResultDTO handler(HttpServletResponse response, EntityNotFoundException e) {

    return ResultDTO.builder().code(HttpStatus.NOT_FOUND.value())
            .msg(e.getLocalizedMessage())
            .build();
  }
}
